package com.ruikong.dazhi.controller.basicinfo;


import com.ruikong.dazhi.controller.result.BaseResult;
import com.ruikong.dazhi.controller.result.ResultApi;
import com.ruikong.dazhi.entity.basicinfo.ShopType;
import com.ruikong.dazhi.model.output.TreeMenuOutput;
import com.ruikong.dazhi.service.basicinfo.IShopTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * (ShopType)表控制层
 */
@RestController
@RequestMapping("/data")
@EnableAutoConfiguration
@Api(tags = "车间类型管理")
public class ShopTypeController {

    private static final Logger logger = LoggerFactory.getLogger(ShopTypeController.class);

    @Autowired
    private IShopTypeService shopTypeService;

    @PostMapping("/getShopTypeById")
    @ApiOperation("根据id获取车间类型信息")
    public BaseResult getShopTypeById(@RequestBody ShopType shopType) {
        BaseResult result = new BaseResult();
        try {
            ShopType shopType1 = shopTypeService.queryById(shopType.getId());
            result.setCode(ResultApi.successCode);
            result.setMsg(ResultApi.getSuccessMsg);
            result.setData(shopType1);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(ResultApi.failCode);
            result.setMsg(ResultApi.getFailMsg);
            logger.error(e.getMessage(), "getShopTypeById");
        }
        return result;
//        return shopTypeService.queryById(shopType.getId());
    }

    /**
     * 获取树形结构
     *
     * @return
     */
    @ApiOperation(value = "获取树形结构")
    @RequestMapping(value = "/getAllShopTypeTree", method = RequestMethod.POST)
    public BaseResult getAllShopTypeTree() {
        BaseResult result = new BaseResult();
        try {
            List<TreeMenuOutput> treeMenuOutput = shopTypeService.queryAllShopTypeTree();
            result.setCode(ResultApi.successCode);
            result.setMsg(ResultApi.getSuccessMsg);
            result.setData(treeMenuOutput);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(ResultApi.failCode);
            result.setMsg(ResultApi.getFailMsg);
            logger.error(e.getMessage(), "getAllShopTypeTree");
        }
        return result;
    }

//    @PostMapping("/getAllShopTypeTree")
//    @ApiOperation("获取所有车间类型信息")
//    public List<ShopType> getAllShopTypeTree() {
//        List<ShopType> shopType = new ArrayList<>();
//        try {
//            shopType = shopTypeService.queryAllShopTypeTree();
//        }catch (Exception e){
//            logger.error("getAllShopTypeTree{}",e.getMessage());
//        }
//        return shopType;
//    }

    /**
     * Description: 新增车间类型信息
     *
     * @Param: [shopType]
     **/
    @PostMapping("/addShopType")
    @ApiOperation("新增车间类型信息")
    public BaseResult addShopType(@RequestBody ShopType shopType) {
        BaseResult result = new BaseResult();
        try {
            if (shopTypeService.isExist(-1, shopType.getShopTypeName()) > 0) {
                result.setCode(ResultApi.failCode);
                result.setMsg("该车间类型已经存在!");
                return result;
            }
            int effect = shopTypeService.insert(shopType);
            if (effect > 0) {
                result.setCode(ResultApi.successCode);
                result.setMsg(ResultApi.addSuccessMsg);
            } else {
                result.setCode(ResultApi.failCode);
                result.setMsg(ResultApi.addFailMsg);
            }
        } catch (Exception e) {
            result.setCode(ResultApi.failCode);
            result.setMsg(ResultApi.addFailMsg);
            logger.error("addShopType{}", e.getMessage());
        }
        return result;
    }

    /**
     * Description: 更新车间信息
     *
     * @Param: [shopType]
     **/
    @PostMapping("/updateShopType")
    @ApiOperation("更新车间类型信息")
    public BaseResult updateShopType(@RequestBody ShopType shopType) {
        BaseResult platformResult = new BaseResult();
        try {
            if (shopTypeService.isExist(shopType.getId(), shopType.getShopTypeName()) > 0) {
                platformResult.setCode(ResultApi.failCode);
                platformResult.setMsg("该车间类型已经存在!");
                return platformResult;
            }
            int effect = shopTypeService.update(shopType);
            if (effect > 0) {
                platformResult.setCode(ResultApi.successCode);
                platformResult.setMsg(ResultApi.updateSuccessMsg);
            } else {
                platformResult.setCode(ResultApi.failCode);
                platformResult.setMsg(ResultApi.updateFailMsg);
            }
        } catch (Exception e) {
            platformResult.setCode(ResultApi.failCode);
            platformResult.setMsg(ResultApi.updateFailMsg);
            logger.error("updateShopType{}", e.getMessage());
        }
        return platformResult;
    }

    /**
     * Description: 删除车间信息
     *
     * @Param: [shopType]
     **/
    @PostMapping("deleteShopType")
    @ApiOperation(value = "删除车间类型信息")
    public BaseResult deleteShopType(@RequestBody ShopType shopType) {
        BaseResult baseResult = new BaseResult();
        try {
            baseResult = this.shopTypeService.deleteById(shopType.getId());
        } catch (Exception e) {
            baseResult.setMsg(ResultApi.deleteFailMsg);
            baseResult.setCode(ResultApi.failCode);
            logger.error("deleteShopType{}", e.getMessage());
        }
        return baseResult;
    }

}